{extend name="base/base" /}
{block name="main"}
<form id="form" method="post" class="layui-form layui-form-pane" enctype="multipart/form-data" action="{:url('/admin/articleEdit')}">
    <div class="layui-form-item">
        <label class="layui-form-label">名称</label>
        <div class="layui-input-block">
            <input type="text" name="name" placeholder="请输入" autocomplete="off" class="layui-input"
                   value="{:isset($list.name)?$list.name:''}"  />
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">所属栏目</label>
        <div class="layui-input-block">
            <select name="category_id" lay-filter="aihao">
                {foreach $category_list as $key=>$vo}
                <option value="{$vo.id}" {if(!empty($list['category_id']) && $vo.id == $list.category_id)} selected="selected"{/if}>{$vo.name}</option>
                {/foreach}
            </select>
        </div>
    </div>

   <div class="layui-form-item">
       <div class="layui-upload">
           <button type="button" class="layui-btn" id="cover">上传图片</button>
           <div class="layui-upload-list">
               <img class="layui-upload-img cover" src="{notempty name="$list.cover_id"} {:getPicture($list.cover_id)}{/notempty}">
               <p id="demoText"></p>
           </div>
       </div>
   </div>
    <input type="hidden" name="cover_id" value="{:isset($list.cover_id)?$list.cover_id:''}">

    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">描述</label>
        <div class="layui-input-block">
            <textarea placeholder="请输入内容" class="layui-textarea" name="describe">{:isset($list.describe)? $list.describe:''}</textarea>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <input type="hidden" name="id" value="{:isset($list['id'])?$list['id']:''}">
            <input type="hidden" name="__token__" id="token" value="{$Request.token}"/>
            <button class="layui-btn" lay-submit lay-filter="*">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
{/block}
{block name="script"}
{css href="/form-select/formSelects-v4.css" /}
<script>
    layui.config({
        base: '/form-select/' //此处路径请自行处理, 可以使用绝对路径
    }).extend({
        formSelects: 'formSelects-v4'
    });
    layui.use(['form', 'jquery', 'formSelects','upload'], function () {
        var formSelects = layui.formSelects, form = layui.form, $ = layui.jquery,upload = layui.upload;
        formSelects.on('selectId', function (id, vals, val, isAdd, isDisabled) {
            $('#group_id').val(layui.formSelects.value(id, 'val'));
        }, true);

        //表单提交
        form.on("submit(*)", function (data) {
            var obj = $(this);
            obj.attr("disabled", "disabled").addClass("layui-disabled");
            $.post(data.form.action, data.field, function (data) {
                var icon = 5;
                if (data.code) {
                    icon = 6;
                }
                layer.msg(data.msg, {icon: icon, time: 1500}, function () {   //提示的插件，可以使用alert代替
                    if (data.code) {
                        setTimeout(function () {
                            //刷新父页面
                            parent.location.reload();
                        }, 500);
                    } else {
                        $('#token').val(data.data.token);
                        obj.removeAttr("disabled").removeClass("layui-disabled");
                    }
                });
            }, 'json');
            return false;
        });


        //绑定原始文件域
        upload.render({
            elem: '#cover',
            url: '{:url("/admin/uploadImage")}',
            accept: 'file',
            exts: 'jpg|jpeg|png|gif',
            acceptMime: 'image/jpg, image/jpeg, image/png, image/gif',
            // data: {
            //     dir: 'cover'
            // },
            before: function(){
                layer.msg('上传中...', {
                    time: 0 //2秒关闭（如果不配置，默认是3秒）
                });
            },
            done: function(res){

                layer.closeAll();
                if(res.code == 0){
                    layer.alert(res.data);
                    return;
                }
                $('.cover').attr('src', 'http://www.origin.com/upload/picture/'+res.data.path);

                $('input[name="cover_id"]').val(res.data.id)
            },

            error: function(res){
                layer.closeAll();
                layer.msg('上传失败');
            }
        });
    });


</script>
{/block}